package top.hongthorn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.hongthorn.pojo.AircraftCarrier;
import top.hongthorn.mapper.AircraftCarrierMapper;
import top.hongthorn.pojo.User;
import top.hongthorn.service.AircraftCarrierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hongthorn.util.Result;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 航母主表 服务实现类
 * </p>
 *
 * @author 泓宇网络科技工作室
 * @since 2023-12-28 00:45:56
 */
@Service
@Transactional
public class AircraftCarrierServiceImpl extends ServiceImpl<AircraftCarrierMapper, AircraftCarrier> implements AircraftCarrierService {


    @Autowired
    private AircraftCarrierMapper aircraftCarrierMapper;


    @Override
    public Result deleteById(Integer id) {
        if (aircraftCarrierMapper.deleteById(id) != 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Map findAircraftCarrier(Integer startIndex, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<AircraftCarrier> aircraftCarrierPage = new Page<>(startIndex, pageSize);
        IPage<AircraftCarrier> iPage = this.page(aircraftCarrierPage, null);
        map.put("code", 0);
        map.put("msg", "true");
        map.put("count", iPage.getTotal());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Result updateAircraftCarrier(AircraftCarrier aircraftCarrier) {
        if (aircraftCarrierMapper.updateById(aircraftCarrier) != 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Result addAircraftCarrier(AircraftCarrier aircraftCarrier) {
        if (aircraftCarrierMapper.findByPortNumber(aircraftCarrier.getPortNumber()) == 1) {
            return Result.error("舷号已存在，请更换一个");
        } else {
            if (aircraftCarrierMapper.insert(aircraftCarrier) != 0) {
                return Result.success("添加成功");
            }
        }
        return Result.error("添加失败");
    }
}